import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';

import 'business_customer_item.dart';

class BusinessCustomer extends StatefulWidget {
  final String businessId;

  /// MyUser or DepotUser
  final String queryType;

  const BusinessCustomer({Key key, @required this.businessId, @required this.queryType}) : super(key: key);

  @override
  State<BusinessCustomer> createState() => _BusinessCustomerState();
}

class _BusinessCustomerState extends State<BusinessCustomer> with AutomaticKeepAliveClientMixin {
  final TextEditingController editingController = TextEditingController();
  final BaseRefreshController refreshController = BaseRefreshController();

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Column(
      children: [
        buildSearchWidget(),
        Expanded(child: buildStyleList()),
      ],
    );
  }

  /// 搜索框
  Widget buildSearchWidget() {
    return Container(
      height: 40,
      margin: EdgeInsets.only(right: 16, left: 16.0, top: 12),
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(4), border: Border.all(color: Color(0xFFEFEFEF))),
      child: BaseSearchFilterComponent.appBar(
        hintText: '请输入姓名/手机号/卡号',
        controller: editingController,
        onEditingComplete: onEditingComplete,
        onClearOut: onClearOut,
        colorModel: BaseSearchFilterColor.light,
      ),
    );
  }

  Widget buildStyleList() {
    return BaseRefreshComponent(
      controller: refreshController,
      path: '/x3-service-mobile-center/vip/operation/home/query/alluser',
      params: {"queryType": widget.queryType, "searchKey": "", "tagWhereValue": ""},
      buildItem: (Map<String, dynamic> item, int index) {
        String vipId = BaseUtils.getKeyData(item, 'vipId');
        return BusinessCustomerItem(key: ValueKey(vipId), item: item, index: index, queryType: widget.queryType);
      },
    );
  }

  /// 搜索框搜索事件 [value] 搜索内容
  void onEditingComplete(String value) async {
    if (value.isEmpty) {
      BaseUtils.showToast('请输入姓名/手机号/卡号');
      return;
    }
    refreshController.refresh(params: {'searchKey': value});
  }

  void onClearOut() async {
    refreshController.refresh(params: {"searchKey": ""});
  }
}
